package com.android.vgongyi.service;

import android.content.Context;

import com.android.vgongyi.activity.WelcomeActivity;
import com.android.vgongyi.common.AppConstant;
import com.android.vgongyi.dao.BaseDAO;
import com.android.vgongyi.dao.ImageDAO;
import com.android.vgongyi.entity.ImageEntity;
import com.android.vgongyi.utils.DBUtil;
import com.android.vgongyi.utils.StringUtil;
import com.android.vgongyi.utils.SystemUtil;

/**
 * if sdcard isexits
 * save image in sdcard
 * or save image in cachedir
 * @author caiying
 *
 */
public class SaveImage {
	private ImageEntity imageEntity = new ImageEntity();
	private ImageDAO imageDAO;
	private String imageUrl = "";
	private String img_extension = "";
	private Context context;

	public SaveImage(Context context, String imageUrl,String img_extension) {
		imageDAO = new ImageDAO(context);
		this.context = context;
		this.imageUrl = imageUrl;
		this.img_extension = img_extension;
	}

	public void save() {
		if(imageUrl.equals("")){
			return ;
		}
		if(!DBUtil.checkisExist(BaseDAO.URI_IMAGE, "imageUrl="+"'"+imageUrl+"'", context)){
			imageEntity.setImageUrl(imageUrl);
			if (WelcomeActivity.SDCARDISEXIST) {
				imageEntity.setImagepath(AppConstant.IMAGE_PATH + "/"
						+ StringUtil.namePicture(imageUrl,img_extension));
			} else {
				imageEntity.setImagepath(context.getCacheDir() + "/"
						+ StringUtil.namePicture(imageUrl,img_extension));
			}
			imageEntity.setImageTime(SystemUtil.getSystemTime());
			imageDAO.insert(imageEntity);
		}
	}

}
